import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:netflix_frontend/common/bloc/generic_data_cubit.dart';
import 'package:netflix_frontend/common/bloc/generic_data_state.dart';
import 'package:netflix_frontend/domain/movie/entities/movie.dart';
import 'package:netflix_frontend/domain/movie/usecases/get_now_playing_movies.dart';
import 'package:netflix_frontend/service_locator.dart';
import 'package:netflix_frontend/common/widgets/movie/movie_card.dart';

class NowPlayingMovies extends StatelessWidget {
  const NowPlayingMovies({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      // create
      create: (context) => GenericDataCubit()
        ..getData<List<MovieEntity>>(sl<GetNowPlayingMoviesUsecase>()),
      // child
      child: BlocBuilder<GenericDataCubit, GenericDataState>(
        // builder
        builder: (context, state) {
          if (state is DataLoading) {
            return const Center(
              child: CircularProgressIndicator(),
            );
          } // if

          if (state is DataLoaded) {
            return SizedBox(
              height: 300,
              // child
              child: ListView.separated(
                scrollDirection: Axis.horizontal,
                padding: const EdgeInsets.symmetric(horizontal: 16),
                itemBuilder: (context, index) {
                  // return MovieCard(
                  //   movieEntity: state.data[index],
                  // );
                },
                separatorBuilder: (context, index) => const SizedBox(
                  width: 10,
                ),
                itemCount: state.data.length, // TODO: what it means
              ),
            );
          } // if

          if (state is FailureLoadData) {
            return Center(
              child: Text(state.errorMessage),
            );
          } // if

          return Container();
        },
      ),
    );
  }
}

// DONE